package com.wjh.controler;

import com.wjh.bean.User;
import com.wjh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@org.springframework.stereotype.Controller

public class UserControl implements Controller {
    @Autowired
    private UserService service;
    @Override
    @RequestMapping("/test")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取参数
        String username=request.getParameter("username");
        //创建modelandview对象
        ModelAndView modelAndView=new ModelAndView();
        User user=new User();
        user.setUsername("wjh");
        user.setMobile("123456");
        //设置数据
        modelAndView.addObject("user",user);
        //设置要转发的路径
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/selectbyuid")
    public String selectuserid(int id, HttpServletRequest req, HttpServletResponse resp, Model model){
        User user=service.getuserbyid(id);
        model.addAttribute("user",user);
        return "index";
    }
    @RequestMapping("/adduser")
    public String adduser(User user){
        service.adduser(user);
        return "index";
    }
    @RequestMapping("/getjson")
    @ResponseBody
    public User getjson(@RequestBody User user){

        System.out.println(user.getUsername());
        user.setUsername("jsonname");
        return user;
    }
}
